﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using GoodMan.Infrastructure.Entities.Repository;
using GoodMan.Infrastructure.Entities.Cache;
using GoodMan.Infrastructure.Entities.Enum;

namespace GoodMan.Infrastructure.Entities.Entity
{
    public class Order : IAccessible<T_Order, Order>, ILogicEntity<T_Order, Order>
    {
        public int Id{get;set;}
        public DateTime OrderDate { get; set; }
        public decimal TotalPrice { get; set; }
        public AreaCode AreaCode { get; set; }
        public OrderStatus Status { get; set; }
        public string UserName { get; set; }
        public string HandleUser { get; set; }
        public string OrderCode { get; set; }
        public string AssemblingInfo { get; set; }
        public string Phone { get; set; }
        public int DormitoryID { get; set; }
        public string ProductDescript { get; set; }
        public PayWay Payway { get; set; }
        public string Remark { get; set; }
        /// <summary>
        /// 商户ID
        /// </summary>
        public int BusinessId { get; set; }

        public string UserRealName { get; set; }
        public Dormitory Dormitory { get; set; }

        protected override void Covariant(T_Order model)
        {
            if (model != null)
            {
                this.AreaCode = (AreaCode)model.AreaCode;
                this.AssemblingInfo = model.AssemblingInfo;
                this.Id = model.Id;
                this.OrderCode = model.OrderCode;
                this.OrderDate = model.OrderDate;
                this.Phone = model.Phone;
                this.Remark = model.Remark;
                this.Status = (OrderStatus)model.Status;
                this.TotalPrice = model.TotalPrice;
                this.UserName = model.UserName;
                this.ProductDescript = model.ProductDescript;
                this.DormitoryID = model.DormitoryID;
                this.Payway = (PayWay)model.Payway;
                this.HandleUser = model.HandleUser;
                this.BusinessId = model.BusinessID;
            }
        }

        protected override T_Order Inverter(Order model)
        {
            if (model != null)
            {
                return new T_Order()
                {
                    AreaCode=(int)model.AreaCode,
                    AssemblingInfo = model.AssemblingInfo,
                    Id = model.Id,
                    OrderCode = model.OrderCode,
                    OrderDate = model.OrderDate,
                    Phone = model.Phone,
                    Remark = model.Remark,
                    Status = (int)model.Status,
                    TotalPrice = model.TotalPrice,
                    UserName = model.UserName,
                    ProductDescript=model.ProductDescript,
                    DormitoryID = model.DormitoryID,
                    Payway=(int)model.Payway,
                    HandleUser = model.HandleUser,
                    BusinessID=model.BusinessId
                };
            }
            return null;
        }

        public Order Set(T_Order dbModel)
        {
            Covariant(dbModel);
            return this;
        }
        public Order LoadDormitory(Dormitory dormitory)
        {
            this.Dormitory = dormitory;
            return this;
        }

        /// <summary>
        /// 将启用缓存,通过username查找
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override Order Find(string key)
        {
            return new OrderCache().GetItem(key);
        }
        public override Order PostDelete()
        {
            new OrderCache().RefreshItem(Id.ToString());
            return base.PostDelete();
        }
        public override Order PostModify()
        {
            new OrderCache().RefreshItem(Id.ToString());
            return base.PostModify();
        }
    }
}
